﻿var groupDetailFormVue = new Vue({
    el: "#groupDetailForm",
    data: {
        isNewGroup: true,
        group: {
            BaseInfo: {
                ID: 0,
                Name: "",
                IsDisable: 0
            },
            UserGroupMappings: []
        },
        userList: [],
        isDisableDictionary: {}
    },
    methods: {
        SaveGroup: function (event) {
            event.preventDefault();
            SaveGroupDetail(this);
        },
        UpdateGroup: function (event) {
            event.preventDefault();
            UpdateGroupDetail(this);
        },
        ListMember: function () {
            ListAllUser(this);
        },
        UpdateMember: function () {
            UpdateGroupMember(this);
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.isDisableDictionary = GlobalDictionary.GetTrueOrFalseDictionary();

        var isNew = GetQueryString("IsNew");
        var groupID = GetQueryString("GroupID");
        if (isNew == "true") {
            this.isNewGroup = true;
        } else {
            this.isNewGroup = false;
            GetGroupDetail(this, groupID);
        }
    },
    watch: {
        group: {
            handler: function (val, oVal) {
                if (val.BaseInfo.Name > 0)
                    CheckGroupName(this);
            },
            deep: true
        }
    }
});

/**
 * 根据ID获得用户组信息
 */
function GetGroupDetail(vue, groupID) {
    parent.ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/Account/GetGroupByID?GroupID=" + groupID + "&v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                vue.group = res.data;
            } else {
                toastr.error(res.message);
            }
            parent.HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            parent.HideAzLoading();
        }
    })
}

/**
 * 保存用户组信息
 */
function SaveGroupDetail(vue) {
    if (CheckGroupDetail(vue)) {
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "POST",
            url: "/Account/SaveGroup",
            data: { group: vue.group },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("保存用户组信息成功User group information saved successfully。");
                    ResetForm(vue);
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    }
}

/**
 * 更新用户组信息
 */
function UpdateGroupDetail(vue) {
    if (CheckGroupDetail(vue)) {
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "POST",
            url: "/Account/UpdateGroup",
            data: { group: vue.group },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("更新用户组信息成功User group information updated successfully。")
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    }
}

/**
 * 检查用户组信息正确性
 */
function CheckGroupDetail(vue) {
    if (!CheckGroupName(vue))
        return false;
    return true;
}

/**
 * 检查是否已填写用户组名称
 */
function CheckGroupName(vue) {
    $("#groupName").removeClass("has-error");
    if (vue.group.BaseInfo.Name == null || vue.group.BaseInfo.Name.length == 0) {
        $("#groupName").addClass("has-error");
        return false;
    }
    return true;
}

/**
 * 清空表单信息
 */
function ResetForm(vue) {
    vue.group = {
        BaseInfo: {
            ID: 0,
            Name: "",
            IsDisable: 0
        },
        UserGroupMappings: []
    }
}

/**
 * 列出所有用户
 */
function ListAllUser(vue) {
    parent.ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/Account/ListUser?v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                vue.userList = res.data;
                vue.$nextTick(function () {
                    //监听Bootstrap中Tab元素显示事件
                    $('a[data-toggle="tab"]').off('shown.bs.tab').on('shown.bs.tab', function (e) {
                        parent.ResizeAzModal();
                        //选中已有成员关系记录
                        $("input[type=checkbox]").each(function () {
                            for (var idx in vue.group.UserGroupMappings) {
                                if (vue.group.UserGroupMappings[idx].UserID == $(this).val()) {
                                    $(this).prop("checked", true);
                                }
                            }
                        });
                    });
                })
            } else {
                toastr.error(res.message);
            }
            parent.HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            parent.HideAzLoading();
            toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
        }
    });
}

/**
 * 重置用户组中成员信息
 */
function ResetUserGroupMappings(vue) {
    if (vue.group != null) {
        vue.group.UserGroupMappings = [];
        $("input[type=checkbox]").each(function () {
            if ($(this).prop("checked") == true) {
                var mapping = {
                    GroupID: vue.group.BaseInfo.ID,
                    UserID: $(this).val()
                }
                vue.group.UserGroupMappings.push(mapping);
            }
        });
    }   
}

/**
 * 更新用户组成员信息
 */
function UpdateGroupMember(vue) {
    if (CheckGroupDetail(vue)) {
        ResetUserGroupMappings(vue);
        parent.ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Account/UpdateGroupUsers",
            data: { group: vue.group },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("更新用户组成员信息成功User group member information updated successfully。")
                } else {
                    toastr.error(res.message);
                }
                parent.HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                parent.HideAzLoading();
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            }
        });
    }
}